package threadLearn;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 重入锁 初探
 * @author 12609
 *
 */
public class ReentrantLockTest {

	public static void main(String[] args) {
		final ExecutorService exec = Executors.newFixedThreadPool(4);
		
		final ReentrantLock lock = new ReentrantLock();
		
		final Condition con = lock.newCondition();
		
		final int time = 5;
		
		final Runnable add = new Runnable() {
			
			@Override
			public void run() {

				System.out.println(Thread.currentThread().getId()+" Pre "+lock);
				lock.lock();
				try {
					//con.await(time, TimeUnit.SECONDS);
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}finally {
					System.out.println(Thread.currentThread().getId()+" Post "+lock.toString());
					lock.unlock();
				}
			}
		};
		for(int i = 0; i < 4; i++) {
			exec.execute(add);
			
		}
		exec.shutdown();
	}
}
